<template>
  <div>
    <my-header
      ><el-breadcrumb-item>通知管理</el-breadcrumb-item>
      <el-breadcrumb-item>通知列表</el-breadcrumb-item></my-header
    >

    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="20"
          ><!-- 日期选择框 -->
          <el-date-picker
            v-model="dateValue"
            type="date"
            placeholder="请选择日期"
            size="large"
            value-format="YYYY-MM-DD"
            clearable
            @change="onSearchList"
            @handleClose="getNoticeList"
        /></el-col>
        <el-col :span="4"
          ><el-button type="primary" @click="addDialogVisible = true"
            >添加通知事件</el-button
          ></el-col
        >
      </el-row>

      <!-- 通知列表区域 -->
      <el-table :data="noticelist" style="width: 100%" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="wenben" label="内容"></el-table-column>
        <el-table-column
          prop="createTime"
          label="修改时间"
          width="250px"
          sortable
        ></el-table-column>
        <el-table-column label="操作" width="100px">
          <template #default="scope">
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="Delete"
              size="small"
              @click="removeNoticeById(scope.row.id)"
            />
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        :page-sizes="[1, 2, 5, 10]"
        small="small"
        background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="noticelist.length"
      />
    </el-card>

    <!-- 添加通知事件的对话框 -->
    <el-dialog v-model="addDialogVisible" title="添加通知" width="50%" @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <el-form
        ref="addFormRef"
        :model="addForm"
        status-icon
        :rules="addFormRules"
        label-width="90px"
      >
        <el-form-item label="通知内容" prop="wenben">
          <el-input v-model="addForm.wenben" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addNotice">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getNotice, addNotice } from '../../api/notice.js'
import axios from 'axios'
export default {
  data() {
    return {
      dateValue: '',
      noticelist: [],
      // 控制添加通知事件对话框的显示与隐藏
      addDialogVisible: false,
      // 控制修改通知事件对话框的显示与隐藏
      editDialogVisible: false,
      // 添加通知事件的表单数据
      addForm: { wenben: '' },
      // 添加表单的验证规则对象
      addFormRules: {
        wenben: [{ required: true, message: '请输入通知内容', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getNoticeList()
  },
  methods: {
    onSearchList() {
      this.noticelist = this.noticelist.filter((item) => item.create_time.includes(this.dateValue))
    },
    async getNoticeList() {
      const { data: res } = await getNotice()
      if (res.code !== 1) {
        return this.$message.error('获取通知列表失败')
      }
      this.noticelist = res.data
    },
    // 监听添加通知事件对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields()
    },
    // 点击按钮，添加新通知
    addNotice() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return
        let params = {}
        params = this.addForm.wenben
        const { data: res } = await addNotice(params)
        if (res.code !== 1) {
          this.$message.error('添加通知失败！')
        }
        // 隐藏添加通知的对话框
        this.$message.success('添加通知成功！')
        // 重新获取通知列表数据
        this.getNoticeList()
        this.addDialogVisible = false
      })
    },
    // 根据id删除对应的通知信息
    async removeNoticeById(id) {
      // 弹框询问通知是否删除
      const confirmResult = await this.$confirm('此操作将永久删除该通知，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => {
        return err
      })
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await axios.delete('/content/info/delete', { data: { id: id } })
      if (res.code !== 1) {
        return this.$message.error('删除通知失败！')
      }
      this.$message.success('删除成功！')
      this.getNoticeList()
    }
  }
}
</script>

<style lang="less" scoped></style>
